package com.jc.oms.controller.role;


import com.jc.common.util.WebUtils;
import com.jc.service.role.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("role")
public class RoleController {


    @Autowired
    private RoleService roleService;

    /**
     * 查询所有的角色
     */
    @RequestMapping("queryRole")
    public Map<String, Object> queryRole(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        try {
            param = WebUtils.ReadAsChars(request);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.roleService.queryRole(param);
    }


    /**
     * 修改角色
     */
    @RequestMapping("roleUpdate")
    public Map<String, Object> roleUpdate(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        try {
            param = WebUtils.ReadAsChars(request);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.roleService.roleUpdate(param);
    }


    /**
     * 新增角色
     */
    @RequestMapping("addRole")
    public Map<String, Object> addRole(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        try {
            param = WebUtils.ReadAsChars(request);
            if (param.size() == 0) {
                result.put("code", 9999);
                result.put("data", "fail");
                result.put("msg", "没有接收到参数");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.roleService.addRole(param);
    }

    /**
     * 修改角色
     */
    @RequestMapping("modifyRoleName")
    public Map<String, Object> modifyRoleName(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        try {
            param = WebUtils.ReadAsChars(request);
            if (param.size() == 0) {
                result.put("code", 9999);
                result.put("data", "fail");
                result.put("msg", "没有接 收到参数");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.roleService.modifyRoleName(param);
    }


    /**
     * 删除角色
     */
    @RequestMapping("delRole")
    public Map<String, Object> delRole(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        try {
            param = WebUtils.ReadAsChars(request);
            if (param.size() == 0) {
                result.put("code", 9999);
                result.put("data", "fail");
                result.put("msg", "没有接收到参数!");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.roleService.delRole(param);
    }

    /**
     * 给角色赋予对应的菜单编号
     */
    @RequestMapping("giveRoleMenu")
    public Map<String, Object> giveRoleMenu(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        try {
            param = WebUtils.ReadAsChars(request);
            if (param.size() == 0) {
                result.put("code", 9999);
                result.put("data", "fail");
                result.put("msg", "没有接收到参数~!");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.roleService.giveRoleMenu(param);
    }

    /**
     * 根据roleId查询用户
     */
    @RequestMapping("queryUserByRoleId")
    public Map<String, Object> queryUserByRoleId(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        try {
            param = WebUtils.ReadAsChars(request);
            if (param.size() == 0) {
                result.put("code", 9999);
                result.put("data", "fail");
                result.put("msg", "没有接收到参数~1!");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.roleService.queryUserByRoleId(param);
    }

    /**
     * 根据roleId userId删除关联
     */
    @RequestMapping("delUserRole")
    public Map<String, Object> delUserRole(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        try {
            param = WebUtils.ReadAsChars(request);
            if (param.size() == 0) {
                result.put("code", 9999);
                result.put("data", "fail");
                result.put("msg", "没有接收到参数~1~!");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.roleService.delUserRole(param);
    }

    /**
     * 根据roleId查询role
     */
    @RequestMapping("queryRoleByRoleId")
    public Map<String, Object> queryRoleByRoleId(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        try {
            param = WebUtils.ReadAsChars(request);
            if (param.size() == 0) {
                result.put("code", 9999);
                result.put("data", "fail");
                result.put("msg", "没有接收到参数~~~~!");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.roleService.queryRoleByRoleId(param);
    }

    /**
     * 根据roleId查询对应的菜单列表
     */
    @RequestMapping("queryMenuListsByRoleId")
    public Map<String, Object> queryMenuListsByRoleId(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        try {
            param = WebUtils.ReadAsChars(request);
            if (param.size() == 0) {
                result.put("code", 9999);
                result.put("data", "fail");
                result.put("msg", "没有接收到参数~~~!~");
                return result;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return this.roleService.queryMenuListsByRoleId(param);
    }

    /**
     * 查询所有的角色包含上下级
     * */
    @PostMapping("queryAllRoles")
    public Map<String,Object> queryAllRoles() {


        return this.roleService.queryAllRoles();
    }
}
